package com.hxd.otc.module.fiat_deal.fragment;

import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.api.Constant;
import com.hxd.otc.base.LazyFragment;
import com.hxd.otc.module.fiat_deal.bean.DealTeamBean;
import com.hxd.otc.module.fiat_deal.contract.SubDealContract;
import com.hxd.otc.module.fiat_deal.presenter.SubDealPresenter;
import com.hxd.otc.module.my.activity.VIPServiceActivity;
import com.hxd.otc.widget.behavior.ByeBurgerFloatButtonBehavior;

import java.util.ArrayList;

import butterknife.BindView;

public class SubDealFragment extends LazyFragment<SubDealPresenter> implements SubDealContract.IView {


    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;


    public static final String EXTRA_ALL_DEAL_TEAM_BEANS = "all_deal_team_beans";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sub_deal;
    }

    @Override
    protected SubDealPresenter createPresenter() {
        return new SubDealPresenter(this);
    }

    @Override
    protected void initData() {
        initTabLayout();
    }

    private void initTabLayout() {
        Bundle arguments = getArguments();
        mPresenter.setDealType(arguments.getInt(DealListFragment.EXTRA_DEAL_TYPE, Constant.Deal.TYPE_BUY));
        mPresenter.setAllDealTeamBeans((ArrayList<DealTeamBean>) arguments.getSerializable(EXTRA_ALL_DEAL_TEAM_BEANS));
        mTabLayout.removeAllTabs();
        mTabLayout.setupWithViewPager(null);
        boolean isFirst = true;
        for (DealTeamBean dealTeamBean : mPresenter.getAllDealTeamBeans()) {
            View indicatorView = View.inflate(getContext(), R.layout.tab_indicator_of_sub_deal, null);
            TextView tvTitle = indicatorView.findViewById(R.id.tv_title);
            tvTitle.setText(dealTeamBean.symbol);
            tvTitle.setTextSize(isFirst ? 16 : 13);
            isFirst = false;
            mTabLayout.addTab(mTabLayout.newTab().setCustomView(indicatorView));
        }

        mViewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            //此方法用来显示tab上的名字
            @Override
            public CharSequence getPageTitle(int position) {
                return mPresenter.getAllDealTeamBeans().get(position).symbol;
            }

            @Override
            public Fragment getItem(int position) {
                DealListFragment dealListFragment = new DealListFragment();
                Bundle bundle = new Bundle();
                bundle.putInt(DealListFragment.EXTRA_DEAL_TYPE, mPresenter.getDealType());
                DealTeamBean dealTeamBean = mPresenter.getAllDealTeamBeans().get(position);
                bundle.putString(DealListFragment.EXTRA_COIN_TYPE, dealTeamBean.symbol);
                bundle.putSerializable(DealListFragment.EXTRA_COIN_INFO, dealTeamBean);
                dealListFragment.setArguments(bundle);
                return dealListFragment;
            }

            @Override
            public int getCount() {
                return mPresenter.getAllDealTeamBeans().size();
            }
        });

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //切换ViewPager
                mViewPager.setCurrentItem(tab.getPosition());
                ((TextView) tab.getCustomView().findViewById(R.id.tv_title)).setTextSize(16);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ((TextView) tab.getCustomView().findViewById(R.id.tv_title)).setTextSize(13);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
    }


    @Override
    protected void initView() {

    }

    @Override
    protected void initEvent() {
    }


    public String getFiat() {
        assert getParentFragment() != null;
        return ((DealFragment) getParentFragment()).getFiat();
    }

    public String getBankTypeValue() {
        assert getParentFragment() != null;
        return ((DealFragment) getParentFragment()).getBankTypeValue();
    }

    public void updateFilterConditions() {
        for (Fragment fragment : getChildFragmentManager().getFragments()) {
            if (fragment instanceof DealListFragment && fragment.isAdded() && fragment.getUserVisibleHint()) {
                ((DealListFragment) fragment).updateFilterConditions();
            }
        }
    }


}